﻿package com.webdesign.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.webdesign.dto.HomeworkInfoDTO;
import com.webdesign.dto.ModifyHomeworkDTO;
import com.webdesign.dto.MyPage;
import com.webdesign.entity.TbTerm;
import com.webdesign.service.ITbHomeworkService;
import com.webdesign.service.ITbTermService;

@Controller("HomeAction")
@Scope("prototype")
public class HomeAction extends ActionSupport {
	
	private ITbHomeworkService iTbHomeworkService;
	
	private ITbTermService iTbTermService;

	private String homeworkid;
	
	private String numpage;
	
	private MyPage page;
	
	private List<TbTerm> terms;
	
	private List<HomeworkInfoDTO> homeworks;
	
	private ModifyHomeworkDTO homework;
	
	
	/*
	 * 点击教师主页后，返回所有学年列表以及当前已经选定学年的所有作业信息，还是留在教室主页
	 */
	public String showTermAndHomework(){
		terms = iTbTermService.showTerms();
		Map<String,Object> session = ActionContext.getContext().getSession();
		String termid = (String) session.get("termid");
		String userid = (String)session.get("userid");
		homeworks =  iTbHomeworkService.showHomework(termid,userid);
		
		if(page == null){
			page = new MyPage();
			page.setNumber(1);
			
		}
		
		page.setTotalElements(homeworks.size());
		if(numpage!=null){
			page.setNumber(Integer.parseInt(numpage));
		}
		page.setTotalElements(homeworks.size());
		homeworks = homeworks.subList((int)(page.getStart()), (int)(page.getEnd()));
		return SUCCESS;
	}

	/*
	 * 根据前台传过来的homework，返回该作业的详情，包括题目，描述，起止时间，评分方式，跳转到修改作业页面
	 */
	public String showHomeworkInfo() {
		homework = iTbHomeworkService.getHomeworkinfo(homeworkid);
		return SUCCESS;
	}

	/*
	 * 教师删除某个已经发布的作业，留在教师主页
	 */
	public String delateHomework() {
		Boolean flag = iTbHomeworkService.delateHomework(homeworkid);
		
		if(flag){
			
			 Map<String,Object> session = ActionContext.getContext().getSession();
			 String termid = (String) session.get("termid");
			 String userid = (String)session.get("userid");
			 homeworks = new ArrayList<HomeworkInfoDTO>();
			 homeworks =  iTbHomeworkService.showHomework(termid,userid);
				
			page = new MyPage();
			String pageNumber = CookieAction.getCookie("pagenumber");
			if(pageNumber != null){
				page.setNumber(Integer.parseInt(pageNumber));
			}else{
				page.setNumber(1);
			}
			
			page.setTotalElements(homeworks.size());
			homeworks = homeworks.subList((int)(page.getStart()), (int)(page.getEnd()));
		}
		return "success";
	}

	public ITbHomeworkService getiTbHomeworkService() {
		return iTbHomeworkService;
	}

	@Resource(name = "TbHomeworkService")
	public void setiTbHomeworkService(ITbHomeworkService iTbHomeworkService) {
		this.iTbHomeworkService = iTbHomeworkService;
	}

	public String getHomeworkid() {
		return homeworkid;
	}

	public void setHomeworkid(String homeworkid) {
		this.homeworkid = homeworkid;
	}

	public ITbTermService getiTbTermService() {
		return iTbTermService;
	}

	@Resource(name="TbTermService")
	public void setiTbTermService(ITbTermService iTbTermService) {
		this.iTbTermService = iTbTermService;
	}
	
	public String getNumpage() {
		return numpage;
	}

	public void setNumpage(String numpage) {
		this.numpage = numpage;
	}

	public MyPage getPage() {
		return page;
	}

	public void setPage(MyPage page) {
		this.page = page;
	}

	public List<TbTerm> getTerms() {
		return terms;
	}

	public void setTerms(List<TbTerm> terms) {
		this.terms = terms;
	}

	public List<HomeworkInfoDTO> getHomeworks() {
		return homeworks;
	}

	public void setHomeworks(List<HomeworkInfoDTO> homeworks) {
		this.homeworks = homeworks;
	}

	public ModifyHomeworkDTO getHomework() {
		return homework;
	}

	public void setHomework(ModifyHomeworkDTO homework) {
		this.homework = homework;
	}
	
	
}
